package com.lanou.musicbox.adapter.localmusic;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.lanou.musicbox.R;
import com.lanou.musicbox.entity.Entity;

import java.util.List;

/**
 * Created by dllo on 15/11/9.
 */
public class CollectAdapter extends BaseAdapter {
    private List<Entity> data;
    private Context context;

    public CollectAdapter(Context context, List<Entity> data) {
        this.context = context;
        this.data = data;
    }

    public void remove(Entity entity) {

        data.remove(entity);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return data != null && data.size() > 0 ? data.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return data != null && data.size() > 0 ? data.get(position) : 0;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.collect_item, null);
            viewHolder = new ViewHolder();
            viewHolder.collect_title = (TextView) convertView.findViewById(R.id.collect_title);
            viewHolder.collect_artist = (TextView) convertView.findViewById(R.id.collect_artist);
            viewHolder.collect_duration = (TextView) convertView.findViewById(R.id.collect_duration);
            viewHolder.collect_num = (TextView) convertView.findViewById(R.id.collect_num);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        Entity entity = (Entity) getItem(position);
        viewHolder.collect_title.setText(entity.getTitle());
        viewHolder.collect_artist.setText(entity.getArtist());
        viewHolder.collect_duration.setText(String.valueOf(formatTime(entity.getDuration())));
        viewHolder.collect_num.setText(position + 1 + ".");
        return convertView;
    }

    public static String formatTime(Long time) {                     //将歌曲的时间转换为分秒的制度
        String min = time / (1000 * 60) + "";
        String sec = time % (1000 * 60) + "";

        if (min.length() < 2)
            min = "0" + min;
        switch (sec.length()) {
            case 4:
                sec = "0" + sec;
                break;
            case 3:
                sec = "00" + sec;
                break;
            case 2:
                sec = "000" + sec;
                break;
            case 1:
                sec = "0000" + sec;
                break;
        }
        return min + ":" + sec.trim().substring(0, 2);
    }

    public class ViewHolder {
        private TextView collect_title;
        private TextView collect_duration;
        private TextView collect_artist;
        private TextView collect_num;
    }
}
